var loanSwitch = $("#loanSwitch").val() && ($("#realonly").val() == 'false');
var bankCardChange = $("#bankCardChange").val();

var dtGridColumns = [{
    id : 'userMobile',
    title : '用户名',
    type : 'string',
    columnClass : 'text-center',
    headerStyle : 'color:grey;'
},{
    id : 'userName',
    title : '借款人姓名',
    type : 'string',
    columnClass : 'text-center',
    headerStyle : 'color:grey;'
},{
    id : 'idcard',
    title : '借款人身份证',
    type : 'string',
    columnClass : 'text-center',
    headerStyle : 'color:grey;'
},{
    id : 'address',
    title : '详细地址',
    type : 'string',
    columnClass : 'text-center',
    headerStyle : 'color:grey;'
},{
    id : 'bank',
    title : '绑定银行',
    type : 'string',
    columnClass : 'text-center',
    headerStyle : 'color:grey;'
},{
    id : 'cardno',
    title : '银行卡号',
    type : 'string',
    columnClass : 'text-center',
    headerStyle : 'color:grey;'
},{
    id : 'spouseName',
    title : '配偶姓名',
    type : 'string',
    columnClass : 'text-center',
    headerStyle : 'color:grey;'
},{
    id : 'spouseMobile',
    title : '配偶手机号',
    type : 'string',
    columnClass : 'text-center',
    headerStyle : 'color:grey;'
},{
    id : 'spouseIdcard',
    title : '配偶身份证',
    type : 'string',
    columnClass : 'text-center',
    headerStyle : 'color:grey;'
},{
    id : 'sxTime',
    title : '授信时间',
    type : 'date',
    format:'yyyy-MM-dd', 
    otype:'time_stamp_ms', 
    oformat:'string',
    columnClass : 'text-center',
    headerStyle : 'color:grey;'
},{
    id : 'agentName',
    title : '授信合伙人',
    type : 'string',
    columnClass : 'text-center',
    headerStyle : 'color:grey;'
},{
    id : 'agentMobile',
    title : '合伙人手机号',
    type : 'string',
    columnClass : 'text-center',
    headerStyle : 'color:grey;'
},{
    id : 'status',
    title : '当前状态',
    type : 'string',
    columnClass : 'text-center',
    headerStyle : 'color:grey;'
},{
    id : 'operation',
    title : '操作',
    type : 'string',
    columnClass : 'text-center tpl-table-black-operation',
    headerStyle : 'color:grey;',
    resolution:function(value, record, column, grid, dataNo, columnNo){
        var content = '';
        if(record.frontPic != null){
        	content += '<a href="#" onclick="check('+"'"+record.frontPic+"'"+')"  > 身份证</a> ';
        }
        if(record.userPic != null){
        	content += '<a href="#" onclick="check('+"'"+record.userPic+"'"+')" >  人脸</a> ';
        }
        if(bankCardChange){
        	content += '<a href="#" onclick="loadPage(\'release/to_change_bankCard/'+record.id+'\');" >  修改银行卡</a> ';
        }
        if(loanSwitch){
        	if(record.loanOnOff=='on'){
            	content += '<a href="#" onclick="disableLoan('+record.id+');" class="" > 禁用</a>';
            }else if(record.loanOnOff=='off'){
            	content += '<a href="#" onclick="ableLoan('+record.id+');" class="" > 启动</a>';
            }
        }
        return content;
    }
},{
    id : 'operation',
    title : '车产',
    type : 'string',
    columnClass : 'text-center tpl-table-black-operation',
    headerStyle : 'color:grey;',
    resolution:function(value, record, column, grid, dataNo, columnNo){
        var content = '';
        content += '<a href="#" onclick="showPic('+record.id+',1'+')"  > 查看</a> ';
        return content;
    }
},{
    id : 'operation',
    title : '房产',
    type : 'string',
    columnClass : 'text-center tpl-table-black-operation',
    headerStyle : 'color:grey;',
    resolution:function(value, record, column, grid, dataNo, columnNo){
        var content = '';
        content += '<a href="#" onclick="showPic('+record.id+',2'+')"  > 查看</a> ';
        return content;
    }
},{
    id : 'operation',
    title : '土地承包证明',
    type : 'string',
    columnClass : 'text-center tpl-table-black-operation',
    headerStyle : 'color:grey;',
    resolution:function(value, record, column, grid, dataNo, columnNo){
        var content = '';
        content += '<a href="#" onclick="showPic('+record.id+',3'+')" > 查看</a> ';
        return content;
    }
}];

var dtGridOption = {
    lang : 'zh-cn',
    ajaxLoad : true,
    check : true,
    myExport : true,
    checkWidth :'37px',
    loadURL : 'loan/loanHandleList',
    columns : dtGridColumns,
    gridContainer : 'dtGridContainer',
    toolbarContainer : 'dtGridToolBarContainer',
    exportURL : 'loan/loanHandleListExport',
    tools : 'export[excel]',
    pageSize : 10,
    pageSizeLimit : [10, 20, 30]
};
var grid = $.fn.dlshouwen.grid.init(dtGridOption);


$(function() {
	grid.load();
	$('.form-datetime-lang').datepicker({
	    language:  'zh-CN',
	    format: 'yyyy-mm-dd',
	    autoclose: true
	});
});

function search(){
	  grid.pager.startRecord = 0;
	  grid.pager.nowPage = 1;
	  grid.parameters = new Object();
	  grid.parameters['homekey'] = $("#homekey").val();
	  grid.parameters['username'] = $("#username").val();
	  grid.parameters['usermobile'] = $("#usermobile").val();
	  grid.parameters['agentname'] = $("#agentname").val();
	  grid.parameters['sxTimeStart'] = $("#sxTimeStart").val();
	  grid.parameters['sxTimeEnd'] = $("#sxTimeEnd").val();
	  grid.parameters['loanerStatus'] = $("#loanerStatus").val();
	  grid.refresh(true);
}

function bankChange(id){
	bankChange("release/to_change_bankCard/"+id,search);
}

function disableLoan(id){
	disable("loan/"+id+"/disableLoan",search);
}

function ableLoan(id){
	able("loan/"+id+"/ableLoan",search)
}

function check(pic){
	$("#imgId").attr('src',pic); 
	$("#imgId").attr('style',"width: 500px;height: 500px;"); 
	$('#doc-modal-1').modal();
}

function showPic(id,type){
	
	loadPage('loan/'+id+'/'+type+'/showPicList')
}


function able(nav,callback){
	layer.confirm('确认启用吗？', {
        icon : 3,
        title : '启用借款权限'
    }, function(index, layero) {
        $.ajax({
            type : "POST",
            url :  nav,
            dataType : "json",
            success : function(resultdata) {
                if (resultdata.ret=="200") {
                    layer.msg(resultdata.message, {
                        icon : 1
                    });
                    if (callback) {
                        callback();
                    }
                } else {
                    layer.msg(resultdata.message, {
                        icon : 0
                    });
                }
            },
            error : function(errorMsg) {
                layer.msg('服务器未响应,请稍后再试', {
                    icon : 3
                });
            }
        });
        layer.close(index);
    });
}

function disable(nav,callback){
	layer.confirm('确认禁用吗？', {
        icon : 3,
        title : '禁用借款权限'
    }, function(index, layero) {
        $.ajax({
            type : "POST",
            url :  nav,
            dataType : "json",
            success : function(resultdata) {
                if (resultdata.ret=="200") {
                    layer.msg(resultdata.message, {
                        icon : 1
                    });
                    if (callback) {
                        callback();
                    }
                } else {
                    layer.msg(resultdata.message, {
                        icon : 0
                    });
                }
            },
            error : function(errorMsg) {
                layer.msg('服务器未响应,请稍后再试', {
                    icon : 3
                });
            }
        });
        layer.close(index);
    });
}


function bankChange(nav,callback){
	$.ajax({
        type : "GET",
        url :  nav,
        dataType : "json",
        success : function(resultdata) {
            if (resultdata.ret=="200") {
                layer.msg(resultdata.message, {
                    icon : 1
                });
                if (callback) {
                    callback();
                }
            } 
        },
        error : function(errorMsg) {
        	
        }
    });
}


